﻿using System;
using System.Web;
using Ornament.Domain;
using Ornament.MemberShip;
using Ornament.MemberShip.Permissions;
using Ornament.Messages;
using Ornament.Web;

namespace Ornament.MVCWebFrame.Areas.MemberShips.Models
{
    public class PermissionWizard
    {
        private readonly string[] _wizardDirect = new[]
                                                      {
                                                          "ChoiceResourceType", //选择资源的类型
                                                          "ChoiceResource", //选择资源
                                                          "EditPermission", //编辑许可证的内容
                                                      };

        private string _descriptionResourceName;

        public PermissionWizard()
        {
            CurrentStep = -1;
        }

        /// <summary>
        /// 当前Wizard的位置
        /// </summary>
        public int CurrentStep { get; set; }

        /// <summary>
        /// 资源的Guid
        /// </summary>
        public string ResourceId { get; set; }

        public Type ResourceType
        {
            get { return OrnamentContext.Configuration.Get(_descriptionResourceName).ValueType; }
        }

        /// <summary>
        /// 资源描述的名称，定义在配置文件中，一般是在Config/WebCfg.config中的
        /// </summary>
        public string DescriptionResourceName
        {
            get { return _descriptionResourceName; }
            set { _descriptionResourceName = value; }
        }

        /// <summary>
        /// 许可证的Id
        /// </summary>
        public Guid PermissionId { get; set; }

        /// <summary>
        /// 表示现在是否在选择资源
        /// </summary>
        public bool IsChoiceResource
        {
            get { return CurrentStep == 1; }
        }

        public bool IsEditPermission
        {
            get { return CurrentStep == 2; }
        }

        public bool IsChoiceResourceType
        {
            get { return CurrentStep == 0; }
        }

        /// <summary>
        /// 上一步
        /// </summary>
        /// <returns></returns>
        public string Previous()
        {
            CurrentStep -= 1;
            return _wizardDirect[CurrentStep];
        }

        /// <summary>
        /// 下一步
        /// </summary>
        /// <returns></returns>
        public string Next()
        {
            CurrentStep += 1;
            return _wizardDirect[CurrentStep];
        }

        public void SetPermission(Permission permission)
        {
            HttpContext.Current.Session["Permission"] = permission;
            if (permission != null)
            {
                var res = permission.Resource as IDomainObject;
                ResourceId = res != null ? res.Id.ToString() : permission.Resource.ToString();

                PermissionId = permission.Id;
                DescriptionResourceName =
                    OrnamentContext.Configuration.GetResourceSettingByType(permission.Resource.GetType()).Name;
            }
        }


        public Permission GetPermission()
        {

            var result = HttpContext.Current.Session["Permission"] as Permission;
            if (result == null)
            {
                if (PermissionId == Guid.Empty)
                {
                    result = Permission.CreatePermission(ResourceType);
                }
                else
                {
                    result =
                        OrnamentContext.Current.GetDaoFactory<IMemberShipFactory>().CreatePermissionDao().Load(
                            PermissionId);
                }
            }

            if (result.Resource == null && ResourceId != null)
            {
                result.Resource = OrnamentContext.GetResource(ResourceType, ResourceId);
            }
            return result;
        }
    }
}